package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.entity.Article;
import com.hoongs.v1.entity.User;
import com.hoongs.v1.mapper.ArticleMapper;
import com.hoongs.v1.mapper.UserMapper;
import com.hoongs.v1.service.ArticleService;
import com.hoongs.v1.utils.JwtUtil;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ArticleServiceImpl implements ArticleService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该文章！";

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result save(Article article) {
        if (StringUtils.isNotBlank(article.getId())) {
            this.update(article);
        } else {
//            this.add(article);
        }
        return null;
    }

    @Override
    public Result add(Article article, String token) {

        Claims claims = null;
        try {
            claims = JwtUtil.parseJWT(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String userId = claims.getSubject();
        User user = userMapper.selectById(userId);
        article.setPubId(userId);
        article.setPubName(user.getUsername());
        if (articleMapper.insert(article) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (articleMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Article article) {
        if (!findArticleById(article.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (articleMapper.updateById(article) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Article article = articleMapper.selectById(id);
        if (article == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(article);
    }

    @Override
    public Result selectPageList(Integer page, Integer size, String title, String tag, String category, Integer status, String[] times) {
        if (!checkPageQuery(page, size)) return Result.error("分页参数错误");

        Page<Article> daoPage = new Page<>(page, size);
        QueryWrapper<Article> wrapper = new QueryWrapper<Article>();
        if (StringUtils.isNotBlank(title)) {
            wrapper.like("title", title);
        }
        if (StringUtils.isNotBlank(tag)) {
            wrapper.like("tag", tag);
        }
        if (StringUtils.isNotBlank(tag)) {
            wrapper.like("tag", tag);
        }
        if (StringUtils.isNotBlank(category)) {
            wrapper.like("category", category);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        if (times != null && times.length != 0 && StringUtils.isNotBlank(times[0]) && StringUtils.isNotBlank(times[1])) {
            wrapper.between("create_time", times[0], times[1]);
        }
        IPage<Article> articleDaoIPage = articleMapper.selectPage(daoPage, wrapper);
        articleDaoIPage.getRecords().forEach(t -> {
            t.setContent(null);
        });
        PResult rs = new PResult(articleDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询文章是否存在
     */
    public Boolean findArticleById(String id) {
        Article article = articleMapper.selectById(id);
        return article != null;
    }

    /**
     * 检查分页参数是否为空
     */
    public Boolean checkPageQuery(Integer page, Integer size) {
        return page != null && page > 0 && size != null && size > 0;
    }

}
